package com.peter.baselibrary.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageInfo
import android.util.Log

@SuppressLint("StaticFieldLeak")
object AppUtils {

    private val TAG = AppUtils::class.java.simpleName
    private var mContext: Context? = null

    val appVersionName: String
        get() {
            var appName = ""
            val info: PackageInfo
            try {
                info = mContext!!.packageManager.getPackageInfo(mContext!!.packageName, 0)
                appName = info.versionName
                Log.d(TAG, "App Version Name: " + appName)
            } catch (e: Exception) {
                Log.e(TAG, "Get app version name error", e)
            }

            return appName
        }
    val appVersionCode: Int
        get() {
            var appCode = 0
            val info: PackageInfo
            try {
                info = mContext!!.packageManager.getPackageInfo(mContext!!.packageName, 0)
                appCode = info.versionCode
                Log.d(TAG, "App Version Code: " + appCode)
            } catch (e: Exception) {
                Log.e(TAG, "Get app version code error", e)
            }

            return appCode
        }

    fun initialize(context: Context) {
        mContext = context
    }

    fun getVersion(context: Context): String? {
        try {
            val packageManager = context.packageManager
            val packInfo = packageManager.getPackageInfo(context.packageName, 0)
            return packInfo.versionName
        } catch (e: Exception) {
            Log.e(TAG, "get version error", e)
        }

        return null
    }

    //    public static void setLocale(Context context) {
    //        PreferenceManager preferenceManager = PreferenceManager.getInstance();
    //        String userLocale = preferenceManager.getLocale();
    //
    //        Resources resources = context.getResources();
    //        Configuration config = resources.getConfiguration();
    //        if (TextUtils.isEmpty(userLocale)) {
    //            String systemLocale = config.locale.toString();
    //            Log.d(TAG, "systemLocale=" + systemLocale);
    //            userLocale = Constants.APP_LANGUAGE_EN;
    //            if (systemLocale.toLowerCase().startsWith(Constants.APP_LANGUAGE_ZH)) {
    //                userLocale = Constants.APP_LANGUAGE_ZH;
    //            }
    //            preferenceManager.setLocale(userLocale);
    //        }
    //
    //        Log.d(TAG, "userLocale=" + userLocale);
    //        config.locale = new Locale(userLocale);
    //        resources.updateConfiguration(config, resources.getDisplayMetrics());
    //    }

    fun getStringIdByName(name: String): Int {
        return mContext!!.resources.getIdentifier(name, "string", mContext!!.packageName)
    }

    fun getDimenIdByName(name: String): Int {
        return mContext!!.resources.getIdentifier(name, "dimen", mContext!!.packageName)
    }

    fun getDrawableIdByName(name: String): Int {
        return mContext!!.resources.getIdentifier(name, "drawable", mContext!!.packageName)
    }

}
